<template>
  <div :class="['playPage', { paused: paused }]" v-if="playMusice">
    <audio
      controls
      autoplay
      :src="`https://music.163.com/song/media/outer/url?id=${playMusice.id}.mp3`"
      @timeupdate="timeupdate"
      @pause="pause"
      @play="play"
      @ended="playEnd"
      ref="player"
    ></audio>
    <div class="miniBar" v-if="playMusice">
      <div class="first">
        <div class="pic" @click="togglePlay">
          <canvas width="50" height="50" ref="miniPlay"></canvas>
          <img :src="playMusice.picUrl" alt="" />
        </div>

        <template>
          <div class="des" @click="isFull = true">
            <h3>
              {{ playMusice.name }} -
              <span v-show="playMusice.artists" v-for="(item,index) in playMusice.artists" :key="index">{{
                item.name
              }}</span>

            </h3>
          </div>
        </template>

        <div class="nextBtn" @click="nextMusic">
          <img src="../assets/nextBtn.png" alt="" />
        </div>
        <div class="songList">
          <img src="../assets/listBtn.png" @click="songListShow" alt="" />
        </div>
      </div>

      <!-- 歌曲列表 -->
      <!-- <div class="songListItem" v-show="songListShow"></div> -->
      <transition
        enter-active-class="animate__animated animate__fadeInUp"
        leave-active-class="animate__animated animate__fadeOutDown"
      >
        <SongListItem
          :musicList="musicList"
          v-show="songListisShow"
          @play="$emit('play', $event)"
        ></SongListItem>
      </transition>
    </div>
    <transition
      enter-active-class="animate__animated animate__fadeInUp"
      leave-active-class="animate__animated animate__fadeOutDown"
    >
      <div class="fullScreen" v-show="isFull">
        <p @click="isFull = false"><img src="../assets/return.png" alt="" /></p>
        <div class="fullPic" @click="songListisShow2 = false">
          <img :src="playMusice.picUrl" />
          <div class="tixing"></div>
          <!-- 暂停上一曲下一曲 -->
          <div class="playBtn" @click="togglePlay"></div>
          <div class="prevBtn" @click="prevMusic"></div>
          <div class="nextBtn" @click="nextMusic"></div>
        </div>
        <div class="songdes">
          <span>{{ playMusice.name }}</span>
        </div>
        <!-- 歌词 -->
        <div class="lyric" @click="songListisShow2 = false">
          <ul
            :style="{
              transform: `translateY(${Litop}px)`,
            }"
          >
            <li
              ref="lyricLi"
              v-for="(ly, index) in lyric"
              :key="index"
              :class="{ lyricLight: index == lyricIndex }"
            >
              {{ ly.lyric }}
            </li>
          </ul>
        </div>
        <!-- 其他按钮 -->
        <div class="control">
          <input
            type="range"
            class="rangeMain"
            max="100"
            v-model="timeRange"
            @change="changeTime"
          />
          <div class="btnList">
            <div
              @click="playModeSet('circle')"
              :class="['circle', { btnListLight: playMode == 'circle' }]"
            >
              <img src="../assets/dqxh.png" alt="" />
            </div>
            <div
              @click="playModeSet('list')"
              :class="['list', { btnListLight: playMode == 'list' }]"
            >
              <img src="../assets/lbxh.png" alt="" />
            </div>
            <div
              @click="playModeSet('rand')"
              :class="['rand', { btnListLight: playMode == 'rand' }]"
            >
              <img src="../assets/sjbf.png" alt="" />
            </div>
            <div class="showlist" @click="songListShow2">
              <img src="../assets/showlist.png" alt="" />
            </div>
          </div>
        </div>
        <!-- <p v-for="item in musicList" :key="item.id">{{ item.name }}</p> -->
        <transition
          enter-active-class="animate__animated animate__fadeInUp"
          leave-active-class="animate__animated animate__fadeOutDown"
        >
          <SongListItem
            :musicList="musicList"
            v-show="songListisShow2"
            style="
              position: absolute;
              bottom: 0px;
              box-shadow: 0px -5px 20px -5px #9e9e9e;
            "
            @play="$emit('play', $event)"
          ></SongListItem>
        </transition>
      </div>
    </transition>
  </div>
</template>

<script>
import SongListItem from "@/components/SongListItem";

export default {
  name: "Play",
  props: {
    playMusice: {
      type: Object,
      default: function () {
        return null;
      },
    },
    musicList: Array,
  },
  data() {
    return {
      paused: false,
      isFull: false,
      lyric: [],
      timeRange: 0,
      duration: 0,
      lyricIndex: 0,
      Litop: 40,
      songListisShow: false,
      songListisShow2: false,
      playMode: "circle",
    };
  },
  created() {
    if (this.playMusice) {
      // console.log(this.playMusice);
      this.getLyric(this.playMusice.id);
    }
    // this.getLyric(this.playMusice.id)
  },
  watch: {
    playMusice(value) {
      this.getLyric(value.id);
    },
  },
  methods: {
    playModeSet(mode) {
      this.playMode = mode;
    },
    playEnd() {
      if (this.playMode == "circle") {
        this.$refs.player.play();
      } else if (this.playMode == "rand") {
        let randIndex = Math.floor(Math.random() * this.musicList.length);
        let randMusic = this.musicList[randIndex];
        this.$emit("play", randMusic);
      } else {
        this.nextMusic();
      }
    },

    prevMusic() {
      let playID = this.playMusice.id;
      let prevMusic = null;

      this.musicList.filter((d, index) => {
        if (d.id == playID) {
          let i = --index;
          if (i < 0) i = this.musicList.length - 1;
          prevMusic = this.musicList[i];
        }
      });
      this.$emit("play", prevMusic);
    },

    nextMusic() {
      let playID = this.playMusice.id;
      // console.log(playID);
      let nextMusic = null;
      this.musicList.filter((d, index) => {
        if (d.id == playID) {
          let i = ++index;
          if (i > this.musicList.length - 1) i = 0;
          nextMusic = this.musicList[i];
        }
      });
      this.$emit("play", nextMusic);
    },

    songListShow() {
      this.songListisShow = !this.songListisShow;
    },
    songListShow2() {
      this.songListisShow2 = !this.songListisShow2;
    },
    getLyric(id) {
      this.$http
        .get("/lyric", {
          params: {
            id: id,
          },
        })
        .then((d) => {
          let lyric = d.data.lrc.lyric;
          let lyricArray = lyric.split("\n");
          let reg = /\[(\d+):(\d+\.\d+)\](.*)/;
          let arr = [];
          for (let i = 0; i < lyricArray.length; i++) {
            let regex = new RegExp(reg);
            if (regex.test(lyricArray[i])) {
              let item = {
                time: parseInt(RegExp.$1) * 60 + parseFloat(RegExp.$2),
                lyric: RegExp.$3,
              };
              arr.push(item);
            }
          }
          this.lyric = arr;
        });
    },
    pause() {
      this.paused = true;
    },

    play() {
      this.paused = false;
    },

    timeupdate(e) {
      let audio = e.target;
      let currentTime = audio.currentTime;
      let duration = audio.duration;
      this.duration = duration;
      this.timeRange = (currentTime / duration) * 100;
      this.drawCircle(currentTime, duration);

      let lyricLi = this.$refs.lyricLi;
      for (var i = 0; i < this.lyric.length; i++) {
        if (i == this.lyric.length - 1) {
          this.lyricIndex = i;
          break;
        }
        if (
          currentTime >= this.lyric[i].time &&
          currentTime < this.lyric[i + 1].time
        ) {
          this.lyricIndex = i;
          this.Litop = -lyricLi[i].offsetTop + 35;
          break;
        }
      }

      // console.log()
    },

    // playStop() {
    //   let player = this.$refs.player;
    //   if (this.paused) {
    //     player.play();
    //   } else {
    //     player.pause();
    //   }
    // },

    togglePlay() {
      let player = this.$refs.player;
      if (this.paused) {
        player.play();
      } else {
        player.pause();
      }
    },

    changeTime(e) {
      let val = e.target.value;
      let player = this.$refs.player;
      let ctime = (val / 100) * this.duration;
      player.currentTime = ctime;
    },

    drawCircle(currentTime, duration) {
      let canvas = this.$refs.miniPlay;
      let context = canvas.getContext("2d");
      context.clearRect(0, 0, 50, 50);
      context.beginPath();
      context.strokeStyle = "rgb(244, 209, 96)";
      context.lineWidth = 5;
      context.arc(25, 25, 20, 0, 2 * Math.PI);
      context.stroke();
      context.closePath();
      let arcP = (currentTime / duration) * 2 * Math.PI;
      context.beginPath();
      context.strokeStyle = "rgb(40, 82, 122)";
      context.lineWidth = 5;
      context.arc(25, 25, 20, 0, arcP);
      context.stroke();
      context.closePath();
    },
  },
  components: {
    SongListItem,
  },
};
</script>


<style lang="less" scoped>
.playPage {
  position: fixed;
  bottom: 0px;
  width: 100%;
  background-color: white;
  z-index: 9;
  border-radius: 20px 20px 0px 0px;
  // height: 65px;
  box-shadow: 0px -5px 20px -5px #9e9e9e;
  .miniBar {
    // height: 100%;
    letter-spacing: 1px;
    .first {
      display: flex;
      padding: 0px 15px;
      align-items: center;
      .pic {
        height: 45px;
        width: 45px;
        border-radius: 50%;
        overflow: hidden;
        position: relative;
        text-align: center;
        // border: 1px solid black;
        img {
          height: 38px;
          width: 38px;
          border-radius: 50%;
          margin-top: 3.5px;
        }
        canvas {
          position: absolute;
          left: -3px;
          top: -2px;
        }
      }
      .pic::after {
        content: "";
        height: 45px;
        width: 45px;
        border-radius: 50%;
        position: absolute;
        left: 0px;
        top: 0px;
        background: url("../assets/begin.png") no-repeat;
        background-position: 11px 10px;
        background-size: 23px 26px;
      }
      .des {
        width: 194px;
        overflow: hidden;
        flex: 6.5;
        margin: 0px 10px;
        height: 60px;
        line-height: 60px;

        h3 {
          font-weight: 400;
          display: inline-block;
          font-size: 16px;
          white-space: nowrap;
          animation: scroll 5s linear infinite normal;
          span {
            // margin-right: 5px;
          }
        }
      }
      .nextBtn,
      .songList {
        height: 35px;
        width: 35px;
        background-color: #e3e6e0;
        line-height: 29px;
        border-radius: 50%;
        text-align: center;
        img {
          height: 23px;
          width: auto;
        }
      }
      .nextBtn {
        margin-right: 10px;
      }
      .songList {
      }
    }
  }
  .fullScreen {
    position: fixed;
    width: 100%;
    bottom: 0px;
    height: 100vh;
    background-color: #fefff5;
    z-index: 9;
    p {
      position: absolute;
      img {
        height: 30px;
        margin-left: 5px;
        margin-top: 15px;
      }
    }
    .fullPic {
      margin: 30px auto;
      width: 300px;
      height: 300px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
        border-radius: 15px;
      }
      .tixing {
        height: 35px;
        width: 140px;
        background-color: #fefff5;
        position: absolute;
        bottom: -1px;
        left: 80px;
        border-radius: 10px 10px 0px 0px;
      }
      .tixing::before,
      .tixing::after {
        content: "";
        position: absolute;
        height: 0px;
        width: 0px;
        bottom: 0px;
        border: 17.5px solid rgba(255, 255, 255, 0);
        border-bottom-color: #fefff5;
      }
      .tixing::before {
        left: -29px;
        border-right-color: #fefff5;
      }
      .tixing::after {
        right: -29px;
        border-left-color: #fefff5;
      }
      .playBtn,
      .prevBtn,
      .nextBtn {
        position: absolute;
        height: 40px;
        width: 40px;
        background-size: 40px;
        background-repeat: no-repeat;
        background-position: center;
      }
      .playBtn {
        background-image: url("../assets/bluestart.png");
        left: 50%;
        margin-left: -20px;
      }
      .prevBtn {
        left: 45px;
        background-image: url("../assets/prev.png");
      }
      .nextBtn {
        right: 45px;
        background-image: url("../assets/next.png");
      }
    }
    .songdes {
      color: #28527a;
      font-size: 22px;
      text-align: center;
      margin: 60px auto 0px;
      letter-spacing: 1px;
      // display: inline-block;
      span {
        position: relative;
        font-weight: bold;
      }
      span::before {
        content: "";
        position: absolute;
        bottom: -1px;
        background-color: #ffc300;
        height: 3px;
        width: 100%;
        border-radius: 10px;
        filter: blur(3px);
      }
    }
    .lyric {
      height: 18vh;
      overflow: hidden;
      text-align: center;
      margin: 18px 0px 15px;
      ul {
        transform: translateY(-10px);
        li {
          height: 29px;
          line-height: 29px;
          font-size: 15px;
          color: #9c9c9c;
        }
        li.lyricLight {
          color: #28527a;
          font-size: 19px;
        }
      }
    }
    .control {
      text-align: center;
      .rangeMain {
        width: 80vw;
        background: #8ac4d0;
        -webkit-appearance: none;
        height: 3px;
      }
      input[type="range"]::-webkit-slider-thumb {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #f4d160;
        box-shadow: 0 0 2px rgba(0, 0, 0, 0.3), 0 3px 5px rgba(0, 0, 0, 0.2);
        -webkit-appearance: none;
      }
      .btnList {
        display: flex;
        width: 80vw;
        margin: 15px auto;
        justify-content: space-around;
        align-items: center;
        .circle,
        .list,
        .rand,
        .showlist {
          img {
            height: 35px;
            margin-top: 1px;
          }
        }
        .showlist {
          img {
            height: 31px;
          }
        }
        .btnListLight {
          border: 2px solid #f4d160;
        }
      }
    }
  }

  audio {
    width: 100%;
    // visibility: hidden;
    display: none;
  }
  @keyframes scroll {
    from {
      transform: translateX(50px);
    }
    to {
      transform: translateX(-50%);
    }
  }
}
.paused {
  .miniBar {
    .pic::after {
      background: url("../assets/stop.png") no-repeat !important;
      background-position: 9.5px 8.1px !important;
      background-size: 30px 30px !important;
    }
  }
  .fullScreen {
    .fullPic {
      .playBtn {
        background-image: url("../assets/bluestop.png");
        background-size: 55px;
        margin-left: -13px;
      }
    }
  }
}
// @keyframes randImg {
//   from {
//     transform: rotate(0deg);
//   }
//   to {
//     transform: rotate(360deg);
//   }
// }
</style>